﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using DevExpress.XtraPivotGrid;
using DistanceLearning.Filters;
using DistanceLearning.Models;
using WebMatrix.WebData;
using CustomFieldDataEventArgs = DevExpress.Web.ASPxPivotGrid.CustomFieldDataEventArgs;
using PivotCellValueEventArgs = DevExpress.Web.ASPxPivotGrid.PivotCellValueEventArgs;

namespace DistanceLearning.Controllers
{
    [InitializeSimpleMembership]
    public class JournalBaseController : Controller
    {
        private UsersContext _usersContext;
        protected UsersContext UsersContext
        {
            get { return _usersContext = (_usersContext ?? new UsersContext()); }
        }

        private JournalContext _journalContext;
        public JournalContext JournalContext
        {
            get
            {
                if (_journalContext != null)
                    return _journalContext;

                var userName = User.Identity.Name;
                if (string.IsNullOrEmpty(userName)) return null;
                var userProfile = UsersContext.UserProfiles.FirstOrDefault(x => x.UserName == userName);
                return _journalContext = new JournalContext(ConnectionHelper.GetSchoolConnection(userProfile.School.SchoolId));
            }
        }

        public int UserId
        {
            get
            {
                var userName = User.Identity.Name;
                var userProfile = UsersContext.UserProfiles.FirstOrDefault(x => x.UserName == userName);
                return userProfile.UserId;
            }
        }

        public UserKind UserKind
        {
            get
            {
                var currentUserId = WebSecurity.CurrentUserId;
                if (Roles.IsUserInRole(JournalStatic.AdminRole))
                    return UserKind.Admin;
                if (Roles.IsUserInRole(JournalStatic.SchoolAdminRole))
                    return UserKind.SchoolAdmin;
                if (JournalContext == null) return UserKind.NotSet;
                if (JournalContext.Teachers.Any(t => t.TeacherID == currentUserId))
                    return UserKind.Teacher;
                if (JournalContext.Parents.Any(p => p.ParentID == currentUserId))
                    return UserKind.Parent;
                if (JournalContext.Pupils.Any(p => p.PupilID == currentUserId))
                    return UserKind.Pupil;
                return UserKind.NotSet;
            }
        }

        public Parent Parent { get { return JournalContext.Parents.FirstOrDefault(p => p.ParentID == WebSecurity.CurrentUserId); } }

        public Teacher Teacher { get { return JournalContext.Teachers.FirstOrDefault(p => p.TeacherID == WebSecurity.CurrentUserId); } }

        public Pupil Pupil { get { return JournalContext.Pupils.FirstOrDefault(p => p.PupilID == WebSecurity.CurrentUserId); } }

        protected void OnCustomGroupInterval(object sender, DevExpress.Web.ASPxPivotGrid.PivotCustomGroupIntervalEventArgs e)
        {
            DateTime data = (DateTime)e.Value;
            //var controller = (JournalController)((MVCxPivotGrid)sender).ViewContext.Controller;

            Quarter[] quarters;
            if (Session["Quarters"] == null)
            {
                quarters = JournalContext.Quarters.ToArray();
                Session["Quarters"] = quarters;
            }
            else
                quarters = Session["Quarters"] as Quarter[];

            if (data >= quarters[0].StartDate && data <= quarters[0].EndDate) e.GroupValue = "I четверть";
            else if (data >= quarters[1].StartDate && data <= quarters[1].EndDate) e.GroupValue = "II четверть";
            else if (data >= quarters[2].StartDate && data <= quarters[2].EndDate) e.GroupValue = "III четверть";
            else if (data >= quarters[3].StartDate && data <= quarters[3].EndDate) e.GroupValue = "IV четверть";
            else e.GroupValue = "Не учебное время";
        }

        protected void OnCustomUnboundFieldData(object sender, CustomFieldDataEventArgs e)
        {
            var index = e.ListSourceRowIndex;
            var source = e.GetListSourceColumnValue("Оценка");
            var field = e.Field;
        }

        protected void OnCustomCellValue(object sender, PivotCellValueEventArgs e)
        {
            //var value = e.Value;
            if (e.ColumnValueType == PivotGridValueType.Value)
            {
                //var value = (double?) e.Value;
            }
        }
    }
}
